<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="主用户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-inFo
                :msg="dataId"
                :disabled="disableSubmit"
                v-decorator="['jianKangXinXiId', validatorRules.jianKangXinXiId]"
                :trigger-change="true"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用户关系" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-inFos
                :disabled="disableSubmit"
                :msg="dataIds"
                v-decorator="['yongHuGuanXi', validatorRules.yongHuGuanXi]"
                :trigger-change="true"
              />
              <!-- <a-input v-decorator="['yongHuGuanXi', validatorRules.yongHuGuanXi]" placeholder="请输入用户关系"></a-input> -->
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="关系" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['guanXi', validatorRules.guanXi]"
                :trigger-change="true"
                dictCode="gkak_qs_guanxi"
                placeholder="请选择关系"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['start', validatorRules.start]"
                :trigger-change="true"
                dictCode="gkak_qs_start"
                placeholder="请选择状态"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
         
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                ecorator="['beuZhu', validatorRules.beuZhu]"
                placeholder="请输入备注"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
         <a-col :span="24">
            <a-form-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor :disabled="disableSubmit" v-decorator="['shuoMing', { trigger: 'input' }]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JEditor from '@/components/jeecg/JEditor'
import JSelectInFo from '@/components/jeecgbiz/JSelectInFo'
import JSelectInFos from '@/components/jeecgbiz/JSelectInFos'

export default {
  name: 'GkakQinShuRelationModal',
  components: {
    JDictSelectTag,
    JEditor,
    JSelectInFo,
    JSelectInFos
  },
  data() {
    return {
      form: this.$form.createForm(this),
      disableSubmit: false,
      title: '操作',
      dataId: {
        id: '',
        name: ''
      },
      dataIds: {
        id: '',
        name: ''
      },
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        jianKangXinXiId: { rules: [{}] },
        yongHuGuanXi: { rules: [{}] },
        guanXi: { rules: [] },
        start: { rules: [] },
        shuoMing: { rules: [] },
        beuZhu: { rules: [] }
      },
      url: {
        add: '/gkakQinShuRelation/add',
        edit: '/gkakQinShuRelation/edit'
      }
    }
  },
  created() {},
  methods: {
    inFoId(record) {
      this.$nextTick(() => {
        this.dataId = {
          id: record.jianKangXinXiId,
          name: record.zhuRealname
        }
        this.dataIds = {
          name: record.guanXiRealname,
          id: record.yongHuGuanXi
        }
      })
    },
    jiGouId(){},
    add() {
      this.edit({})
    },
    edit(record) {
      
      if (this.$route.query.jianKangXinXiId) {
        if (localStorage.getItem('jiangkangInfo')) {
          let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
          console.log(row)
          this.dataId = {
          id: row.id,
          name:row.xingMing
        }
         
        }
      } else {
        this.inFoId(record)
      }
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(this.model, 'jianKangXinXiId', 'yongHuGuanXi', 'guanXi', 'start', 'shuoMing', 'beuZhu')
        )
      })
    },
    close() {
      this.$emit('close')
      this.dataId = {}
      this.dataIds = {}
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          formData.jianKangXinXiId = this.$route.query.jianKangXinXiId

          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'jianKangXinXiId', 'yongHuGuanXi', 'guanXi', 'start', 'shuoMing', 'beuZhu'))
    }
  }
}
</script>
